package com.citrix.gs.test.perforce;

import java.util.List;
import java.util.function.Consumer;

import com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.FileSpecOpStatus;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.AccessException;
import com.perforce.p4java.exception.ConnectionException;
import com.perforce.p4java.server.IServer;

public class ClientUsageDemo extends P4JavaDemo {

    public static void main(String[] args) {
        
        IServer server = null;
        try {
          server = getServer(null);
          server.setUserName(userName);
          server.login(password);
          IClient client = server.getClient(clientName);
          if (client != null) {
              server.setCurrentClient(client);
              System.out.println(client.getHostName());
              
              List<IFileSpec> haveList = client.haveList(
                      FileSpecBuilder.makeFileSpecList("//..."));
              
              haveList.forEach(new Consumer<IFileSpec>() {

                    @Override
                    public void accept(IFileSpec f) {
                       if(f != null) {
                           if(f.getOpStatus() == FileSpecOpStatus.VALID) {
                               System.out.println("have: " + 
                                       f.getDepotPath());
                           }
                           else {
                               System.err.println(f.getStatusMessage());
                           }
                       }  
                    }
              });
              
          } else {
              System.out.println("client is null");
          }
        } catch (Exception exc) {
          exc.printStackTrace();
        }finally {
            if(server != null) {
                try {
                    server.disconnect();
                } catch (ConnectionException e) {
                    e.printStackTrace();
                } catch (AccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
